package com.tian.controller;


import com.tian.pojo.bo.ShopcartBO;
import com.tian.utils.JSONResult;
import com.tian.utils.JsonUtils;
import com.tian.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(value = "购物车接口Controller",tags = "购物车接口相关的Api")
@RestController
@RequestMapping("shopcart")
public class ShopcartController extends BaseController{

    @Autowired
    private RedisOperator redisOperator;


    @ApiOperation(value = "添加商品到购物车",notes = "添加商品到购物车",httpMethod = "POST")
    @PostMapping("/add")
    public JSONResult add(
            @RequestParam String userId,
            @RequestBody ShopcartBO shopcartBO,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("");
        }
        System.out.println(shopcartBO);

        // 前端用户在登录的情况下，添加商品到购物车，会同时在后端同步购物车到redis
        // 需要判断当前购物车中包含已经存在的商品，如果存在则累加购买数量
        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        List<ShopcartBO> shopcartBOList =null;
        if (StringUtils.isNotBlank(shopcartJson)){
            //redis中已经有购物车了
            shopcartBOList= JsonUtils.jsonToList(shopcartJson,ShopcartBO.class);
            //判断购物车中是否存在已有商品，如果有的话counts累加
            boolean isHaving =false;
            for (ShopcartBO sc:shopcartBOList){
                String tmpSpecId =sc.getSpecId();
                if (tmpSpecId.equals(shopcartBO.getSpecId())){
                    sc.setBuyCounts(sc.getBuyCounts()+shopcartBO.getBuyCounts());
                    isHaving=true;
                }
            }
            if (!isHaving){
                shopcartBOList.add(shopcartBO);
            }
        }else {
            //redis中没有购物车
            shopcartBOList=new ArrayList<>();
            //直接添加到购物车中
            shopcartBOList.add(shopcartBO);
        }

        //覆盖现有redis中的购物车
        redisOperator.set(FOODIE_SHOPCART+":"+userId,JsonUtils.objectToJson(shopcartBOList));


        return JSONResult.ok();
    }

    @ApiOperation(value = "从购物车中删除商品",notes = "从购物车中删除商品",httpMethod = "POST")
    @PostMapping("/del")
    public JSONResult del(
            @RequestParam String userId,
            @RequestParam String itemSpecId,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)){
            return JSONResult.errorMsg("参数不能为空");
        }
        // 用户在页面删除购物车中的商品数据，如果此时用户已经登录，则需要删除redis购物车中商品
        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        if (StringUtils.isNotBlank(shopcartJson)){
            //redis中已经有购物车了
            List<ShopcartBO> shopcartBOList = JsonUtils.jsonToList(shopcartJson, ShopcartBO.class);
            //判断购物车中是否存在已有商品，如果有的话则删除
            for (ShopcartBO shopcartBO:shopcartBOList){
                if (shopcartBO.getSpecId().equals(itemSpecId)){
                    shopcartBOList.remove(shopcartBO);
                    break;
                }
            }
            //覆盖现有redis中的购物车
            redisOperator.set(FOODIE_SHOPCART+":"+userId,JsonUtils.objectToJson(shopcartBOList
            ));
        }


        return JSONResult.ok();
    }
}
